#!/usr/bin/env bash
# Check that the Cobbler HTTP endpoint /settings/ is callable

source ${SYSTESTS_PRELUDE} && prepare

set -x -e -o pipefail

# Arrange
cobbler distro add --name fake --arch x86_64 --kernel ${fake_kernel} \
	--initrd ${fake_initramfs}
cobbler profile add --name fake --distro fake
cobbler system add --name testbed --profile fake
# Prepare expected result
cat >${tmp}/a <<-EOF
{
    "allow_duplicate_hostnames": false,
    "allow_duplicate_ips": false,
    "allow_duplicate_macs": false,
    "allow_dynamic_settings": false,
    "always_write_dhcp_entries": false,
    "anamon_enabled": false,
    "auth_token_expiration": 3600,
    "authn_pam_service": "login",
    "autoinstall_snippets_dir": "/var/lib/cobbler/snippets",
    "autoinstall_templates_dir": "/var/lib/cobbler/templates",
    "bind_chroot_path": "",
    "bind_zonefile_path": "/var/lib/named",
    "bind_master": "127.0.0.1",
    "boot_loader_conf_template_dir": "/etc/cobbler/boot_loader_conf",
    "bootloaders_dir": "/var/lib/cobbler/loaders",
    "bootloaders_shim_folder": "/usr/share/efi/*/",
    "bootloaders_shim_file": "shim\\.efi",
    "bootloaders_ipxe_folder": "/usr/share/ipxe/",
    "grubconfig_dir": "/var/lib/cobbler/grub_config",
    "build_reporting_enabled": false,
    "build_reporting_email": [
        "root@localhost"
    ],
    "build_reporting_ignorelist": [],
    "build_reporting_sender": "",
    "build_reporting_smtp_server": "localhost",
    "build_reporting_subject": "",
    "buildisodir": "/var/cache/cobbler/buildiso",
    "cheetah_import_whitelist": [
        "random",
        "re",
        "time",
        "netaddr"
    ],
    "client_use_https": false,
    "client_use_localhost": false,
    "cobbler_master": "",
    "convert_server_to_ip": false,
    "createrepo_flags": "-c cache -s sha",
    "autoinstall": "default.ks",
    "default_name_servers": [],
    "default_name_servers_search": [],
    "default_ownership": [
        "admin"
    ],
    "default_password_crypted": "$1$mF86/UHC$WvcIcX2t6crBz2onWxyac.",
    "default_template_type": "cheetah",
    "default_virt_bridge": "xenbr0",
    "default_virt_disk_driver": "raw",
    "default_virt_file_size": 5,
    "default_virt_ram": 512,
    "default_virt_type": "xenpv",
    "enable_ipxe": false,
    "enable_menu": true,
    "http_port": 80,
    "include": [
        "/etc/cobbler/settings.d/*.settings"
    ],
    "iso_template_dir": "/etc/cobbler/iso",
    "jinja2_includedir": "/var/lib/cobbler/jinja2",
    "kernel_options": {},
    "ldap_anonymous_bind": true,
    "ldap_base_dn": "DC=example,DC=com",
    "ldap_port": 389,
    "ldap_search_bind_dn": "",
    "ldap_search_passwd": "",
    "ldap_search_prefix": "uid=",
    "ldap_server": "ldap.example.com",
    "ldap_tls": true,
    "ldap_tls_cacertdir": "",
    "ldap_tls_cacertfile": "",
    "ldap_tls_certfile": "",
    "ldap_tls_keyfile": "",
    "ldap_tls_reqcert": "",
    "ldap_tls_cipher_suite": "",
    "bind_manage_ipmi": true,
    "manage_dhcp": true,
    "manage_dhcp_v6": false,
    "manage_dhcp_v4": true,
    "manage_dns": false,
    "manage_forward_zones": [],
    "manage_reverse_zones": [],
    "manage_genders": false,
    "manage_rsync": false,
    "manage_tftpd": true,
    "mgmt_classes": [],
    "mgmt_parameters": {
        "from_cobbler": true
    },
    "next_server_v4": "192.168.1.1",
    "next_server_v6": "::1",
    "nsupdate_enabled": false,
    "nsupdate_log": "/var/log/cobbler/nsupdate.log",
    "nsupdate_tsig_algorithm": "hmac-sha512",
    "nsupdate_tsig_key": [
        "cobbler_update_key.",
        "hvnK54HFJXFasHjzjEn09ASIkCOGYSnofRq4ejsiBHz3udVyGiuebFGAswSjKUxNuhmllPrkI0HRSSmM2qvZug=="
    ],
    "power_management_default_type": "ipmilan",
    "proxies": [],
    "proxy_url_ext": "",
    "proxy_url_int": "",
    "puppet_auto_setup": false,
    "puppet_parameterized_classes": true,
    "puppet_server": "",
    "puppet_version": 2,
    "puppetca_path": "/usr/bin/puppet",
    "pxe_just_once": true,
    "nopxe_with_triggers": true,
    "redhat_management_permissive": false,
    "redhat_management_server": "xmlrpc.rhn.redhat.com",
    "redhat_management_key": "",
    "register_new_installs": false,
    "remove_old_puppet_certs_automatically": false,
    "replicate_repo_rsync_options": "-avzH",
    "replicate_rsync_options": "-avzH",
    "reposync_flags": "--newest-only --delete --refresh --remote-time",
    "reposync_rsync_flags": "-rltDv --copy-unsafe-links",
    "restart_dhcp": true,
    "restart_dns": true,
    "run_install_triggers": true,
    "scm_track_enabled": false,
    "scm_track_mode": "git",
    "scm_track_author": "cobbler <cobbler@localhost>",
    "scm_push_script": "/bin/true",
    "serializer_pretty_json": false,
    "server": "192.168.1.1",
    "sign_puppet_certs_automatically": false,
    "signature_path": "/var/lib/cobbler/distro_signatures.json",
    "signature_url": "https://cobbler.github.io/signatures/3.0.x/latest.json",
    "syslinux_dir": "/usr/share/syslinux",
    "syslinux_memdisk_folder": "/usr/share/syslinux",
    "syslinux_pxelinux_folder": "/usr/share/syslinux",
    "tftpboot_location": "/srv/tftpboot",
    "virt_auto_boot": true,
    "webdir": "/srv/www/cobbler",
    "webdir_whitelist": [
        "misc",
        "web",
        "webui",
        "localmirror",
        "repo_mirror",
        "distro_mirror",
        "images",
        "links",
        "pub",
        "repo_profile",
        "repo_system",
        "svc",
        "rendered",
        ".link_cache"
    ],
    "xmlrpc_port": 25151,
    "yum_distro_priority": 1,
    "yum_post_install_mirror": true,
    "yumdownloader_flags": "--resolve",
    "windows_enabled": false,
    "windows_template_dir": "/etc/cobbler/windows",
    "samba_distro_share": "DISTRO",
    "auto_migrate_settings": false,
    "bootloaders_formats": {
        "aarch64": {
            "binary_name": "grubaa64.efi"
        },
        "arm": {
            "binary_name": "bootarm.efi"
        },
        "arm64-efi": {
            "binary_name": "grubaa64.efi",
            "extra_modules": [
                "efinet"
            ]
        },
        "i386-efi": {
            "binary_name": "bootia32.efi"
        },
        "i386-pc-pxe": {
            "binary_name": "grub.0",
            "mod_dir": "i386-pc",
            "extra_modules": [
                "chain",
                "pxe",
                "biosdisk"
            ]
        },
        "i686": {
            "binary_name": "bootia32.efi"
        },
        "IA64": {
            "binary_name": "bootia64.efi"
        },
        "powerpc-ieee1275": {
            "binary_name": "grub.ppc64le",
            "extra_modules": [
                "net",
                "ofnet"
            ]
        },
        "x86_64-efi": {
            "binary_name": "grubx64.efi",
            "extra_modules": [
                "chain",
                "efinet"
            ]
        }
    },
    "bootloaders_modules": [
        "btrfs",
        "ext2",
        "xfs",
        "jfs",
        "reiserfs",
        "all_video",
        "boot",
        "cat",
        "configfile",
        "echo",
        "fat",
        "font",
        "gfxmenu",
        "gfxterm",
        "gzio",
        "halt",
        "iso9660",
        "jpeg",
        "linux",
        "loadenv",
        "minicmd",
        "normal",
        "part_apple",
        "part_gpt",
        "part_msdos",
        "password_pbkdf2",
        "png",
        "reboot",
        "search",
        "search_fs_file",
        "search_fs_uuid",
        "search_label",
        "sleep",
        "test",
        "true",
        "video",
        "mdraid09",
        "mdraid1x",
        "lvm",
        "serial",
        "regexp",
        "tr",
        "tftp",
        "http",
        "luks",
        "gcry_rijndael",
        "gcry_sha1",
        "gcry_sha256"
    ],
    "grub2_mod_dir": "/usr/share/grub2"
}EOF

# Act
curl --output ${tmp}/b http://localhost/cblr/svc/op/settings

# Assert
diff ${tmp}/{a,b}